package com.easyskool.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.easyskool.constants.CommonConstants;
import com.easyskool.constants.DataBaseConstants;
import com.easyskool.utilfunction.UtilFunctions;

/**
 * Servlet Filter implementation class RedirectionFilter
 */
@WebFilter("/RedirectionFilter")
public class RedirectionFilter implements Filter {

	/**
	 * Default constructor.
	 */
	public RedirectionFilter() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) after
	 *      login it takes into account the role specified in the session and
	 *      redirects accordingly
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// This part cheks if the user is signed in or not
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		HttpSession session = req.getSession(true);
		String agCode = (String) session.getAttribute("accessGroup");
		String defaultPage= (String) session.getAttribute("defaultPage");
		System.out.println("Roles in redirectioon Filter " + agCode);
		if (agCode == null)  
			res.sendRedirect(CommonConstants.LOGIN_URL);
		// This part cheks if  the user is signed in or not
		else
		{
			chain.doFilter(request, response);
		}
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
